﻿using System;
using Microsoft.AspNetCore.Mvc;

namespace HzyAdmin.Areas.Admin.Controllers.Sys
{
    //
    using Model;
    using BLL;
    using DbFrame.Class;
    using Common;

    /// <summary>
    /// 角色功能
    /// </summary>
    public class RoleFunctionController : BaseController
    {

        protected override void Init()
        {
            this.MenuID = "Z-140";
        }

        Sys_RoleM _Sys_RoleM = new Sys_RoleM();
        Sys_MenuM _Sys_MenuM = new Sys_MenuM();
        Sys_MenuBL _Sys_MenuBL = new Sys_MenuBL();
        Sys_MenuFunctionM _Sys_MenuFunctionM = new Sys_MenuFunctionM();
        Sys_FunctionM _Sys_FunctionM = new Sys_FunctionM();
        Sys_RoleMenuFunctionM _Sys_RoleMenuFunctionM = new Sys_RoleMenuFunctionM();

        #region  基本操作，增删改查

        /// <summary>
        /// 获取角色菜单功能
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult GetRoleMenuFunctionTree(string roleid)
        {
            return Json(new { status = 1, value = _Sys_MenuBL.GetRoleMenuFunctionTree(roleid) });
        }

        /// <summary>
        /// 保存角色功能
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Save(string rows, string roleid)
        {
            if (roleid.GetGuid() == Guid.Empty)
                throw new MessageBox("请选择角色");

            if (!db.Delete<Sys_RoleMenuFunctionM>(w => w.RoleMenuFunction_RoleID == roleid.GetGuid(), li))
                throw new MessageBox(db.ErrorMessge);

            db.JsonToList<Sys_MenuFunctionM>(rows).ForEach(item =>
            {
                db.Add<Sys_RoleMenuFunctionM>(new Sys_RoleMenuFunctionM
                {
                    RoleMenuFunction_MenuID = item.MenuFunction_MenuID,
                    RoleMenuFunction_FunctionID = item.MenuFunction_FunctionID,
                    RoleMenuFunction_RoleID = roleid.GetGuid()
                }, li);
            });

            if (!db.Commit(li))
                throw new MessageBox(db.ErrorMessge);
            return Json(new { status = 1 });
        }
        #endregion 基本操作，增删改查


    }
}